#include "time_mgr.h"
#include "io_mgr.h"
#include "pmc.h"
#include "sysclk.h"
#include "tc.h"

#define SYSTEM_TIMER_FREQ   1000

uint32_t system_ms_tick = 0;

time_slice_t time_slice_vector[14] =
{
  {false, 0},
  {false, 0},
  {false, 0},
  {false, 0},
  {false, 0},
  {false, 0},
  {false, 0},
  {false, 0},
  {false, 0},
  {false, 0},
  {false, 0},
  {false, 0},
  {false, 0},
  {false, 0}
};



//uint32_t g_check_start_line;

//uint32_t g_nocharge_led_start_line;
//uint32_t g_flick_led_start_line;
//uint32_t g_charge_led_start_line;

//uint32_t g_re_pc_start_line;

//uint32_t g_sleep_start_line;

//uint32_t g_delay_start_line;

void system_timer_interrupt(void)
{
    uint32_t ul_status;
    ul_status = tc_get_status(TC0, 0);
    if ((ul_status & TC_SR_CPCS) == TC_SR_CPCS)  
    {
        system_ms_tick += (1000 / SYSTEM_TIMER_FREQ);
    }
    
    if(system_ms_tick > 4294960000)
    {
        reset_system();
    }
}

void start_system_timer(uint32_t cpu_hz)
{
    uint32_t ul_div;
    uint32_t ul_tcclks;
    pmc_enable_periph_clk(ID_TC0);
        
    NVIC_SetPriority((IRQn_Type)ID_TC0, 1);
    NVIC_EnableIRQ((IRQn_Type)ID_TC0);
    
    tc_find_mck_divisor(SYSTEM_TIMER_FREQ, cpu_hz, &ul_div, &ul_tcclks, cpu_hz);  
    tc_init(TC0, 0, ul_tcclks | TC_CMR_CPCTRG);  
    tc_write_rc(TC0, 0, (cpu_hz / ul_div) / SYSTEM_TIMER_FREQ);
    
    tc_enable_interrupt(TC0, 0, TC_IER_CPCS);
    tc_start(TC0, 0);
}

void stop_system_timer(void)
{
    tc_stop(TC0, 0);
}

void start_time_slice(uint8_t idx)
{
    if(time_slice_vector[idx].b_valid == false)
    {
        time_slice_vector[idx].b_valid = true;
        time_slice_vector[idx].start_line = get_system_time();
    }
}

void reset_time_slice(uint8_t idx)
{
    if(time_slice_vector[idx].b_valid == true)
    {
        time_slice_vector[idx].start_line = get_system_time();
    }
}

bool Is_time_slice_reach(uint8_t idx, uint32_t slice_value)
{
    if((get_system_time() - time_slice_vector[idx].start_line >= slice_value) && (time_slice_vector[idx].b_valid == true))
    {
        return true;
    }
    
    else
    {
        return false;
    }
}

void start_led_timer(void)
{
    if(ex_status.usb_cnct_status == HUB_CNCT)
    {
        start_charge_led_timer();
    }
    else
    {
        start_nocharge_led_timer();
        g_power_led_idx = LED_1;
    }
}

/*void start_check_timer(void)
{
    if(g_check == 0)
    {
        g_check = 1;
        g_check_start_line = get_system_time();
    }
}*/

/*void stop_check_timer(void)
{
    g_check = 0;
}*/

/*void reset_check_timer(void)
{
    if(g_check == 1)
    {
        g_check_start_line = get_system_time();
    }
}*/

/*bool Is_check_time_over(void)
{
    if((get_system_time() - g_check_start_line >= CHECK_TIME_SLICE) && (g_check == 1))
    {
        return true;
    }
    
    else
    {
        return false;
    }
}*/

/*void start_re_pc_timer(void)
{
    g_re_pc_wait = 1;
    g_re_pc_start_line = get_system_time();
}*/

/*bool Is_re_pc_time_over(void)
{
    if((get_system_time() - g_re_pc_start_line > 3000) && (g_re_pc_wait == 1))
    {
        return true;
    }
    else
    {
        return false;
    }
}*/

/*void start_charge_led_timer(void)
{
    if(g_charge_led == 0)
    {
        g_charge_led = 1;         
        g_charge_led_start_line = get_system_time();
    }   
}*/

/*void start_next_fick(void)
{
    g_flick_led_start_line = get_system_time();
}*/

/*bool flick_time_over(void)
{
    if((get_system_time() - g_flick_led_start_line >= LED_FLICK_TIME_SLICE) && (g_charge_led == 1))
    {
        start_next_fick();
        return true;
    }
    else
    {
        return false;
    }
}*/

/*bool charge_led_time_over(void)
{
    if((get_system_time() - g_charge_led_start_line >= LED_CONTINUE_TIME) && (g_charge_led == 1))
    {
        g_charge_led = 0;
        return true;
    }
    else
    {
        return false;
    }
}*/

/*bool charge_led_start_over(void)
{
    if((get_system_time() - g_charge_led_start_line >= LED_1S_SLICE) && (g_charge_led == 1))
    {
        return true;
    }
    else
    {
        return false;
    }
}*/

/*void start_nocharge_led_timer(void)
{
    if(g_nocharge_led == 0)
    {
        g_nocharge_led = 1;         
        g_nocharge_led_start_line = get_system_time();
        g_power_led_idx = LED_1;
    }
}*/

/*bool nocharge_led_time_over(uint32_t time_slice)
{
    if((get_system_time() - g_nocharge_led_start_line >= time_slice) && (g_nocharge_led == 1))
    {
        return true;
    }
    else
    {
        return false;
    }
}*/

/*void reset_nocharge_led_timer(void)
{
    if(g_nocharge_led == 1)
    {
        g_nocharge_led_start_line = get_system_time();
    }
}*/

/*void start_delay_timer(void)
{
    if(g_start_delay == 0)
    {
        g_start_delay = 1;
        g_delay_start_line = get_system_time();
    }
}*/

/*bool delay_time_over(uint32_t delay_ms)
{
    if((get_system_time() - g_delay_start_line >= delay_ms) && (g_start_delay == 1))
    {
        g_start_delay = 0;
        return true;
    }
    else
    {
        return false;
    }
}*/

/*void stop_delay_timer(void)
{
    g_start_delay = 0;
}*/

/*void start_sleep_timer(void)
{
    if(g_start_sleep == 0)
    {
        g_start_sleep = 1;
        g_sleep_start_line = get_system_time();
    }    
}*/

/*void stop_sleep_timer(void)
{
    g_start_sleep = 0;
}*/

/*bool sleep_time_over(void)
{
    if((get_system_time() - g_sleep_start_line >= SLEEP_TIME_SLICE) && (g_start_sleep == 1))
    {
        return true;
    }
    else
    {
        return false;
    }
}*/

bool Is_sleep_time_over(void)
{
    if(ex_status.usb_role == USB_CONFIGED)
    {
        return Is_time_slice_reach(SLEEP_SLICE, SLEEP_TIME_SLICE_LONG);
    }
    else
    {
        return Is_time_slice_reach(SLEEP_SLICE, SLEEP_TIME_SLICE_SHORT);
    }
}

/*void reset_sleep_timer(void)
{
    if(g_start_sleep == 1)
    {
        g_sleep_start_line = get_system_time();
    }
}*/


